package org.little.v3;

import org.junit.Assert;
import org.junit.Test;
import org.little.beans.BeanDefinition;
import org.little.beans.ConstructorArgument;
import org.little.beans.factory.config.RuntimeBeanReference;
import org.little.beans.factory.config.TypedStringValue;
import org.little.beans.factory.support.DefaultBeanFactory;
import org.little.beans.factory.xml.XmlBeanDefinitionReader;
import org.little.core.io.ClassPathResource;

import java.util.List;

/**
 * @author Tinyspot
 * @date 2022-05-22 12:43
 */
public class BeanDefinitionTest3 {

    @Test
    public void testConstructorArgument() {
        DefaultBeanFactory factory = new DefaultBeanFactory();
        XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(factory);
        reader.loadBeanDefinitions(new ClassPathResource("little-v3.xml"));

        BeanDefinition beanDefinition = factory.getBeanDefinition("petStore");
        Assert.assertEquals("org.little.service.v3.PetStoreService", beanDefinition.getBeanClassName());

        ConstructorArgument argument = beanDefinition.getConstructorArgument();
        List<ConstructorArgument.ValueHolder> valueHolders = argument.getArgumentValues();
        Assert.assertEquals(3, valueHolders.size());

        RuntimeBeanReference reference = (RuntimeBeanReference) valueHolders.get(0).getValue();
        Assert.assertEquals("accountDao", reference.getBeanName());

        RuntimeBeanReference reference2 = (RuntimeBeanReference) valueHolders.get(1).getValue();
        Assert.assertEquals("itemDao", reference2.getBeanName());

        TypedStringValue value = (TypedStringValue) valueHolders.get(2).getValue();
        Assert.assertEquals("1", value.getValue());


    }
}
